#include "settingsdialog.h"
#include "ui_settingsdialog.h"

SettingsDialog::SettingsDialog(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SettingsDialog)
{
    ui->setupUi(this);
    // 初始化 lineEdit
    ui->lineEdit->setText(QString::number(Widget::temperatureThreshold));
    updateLineEditState(false);
}

SettingsDialog::~SettingsDialog()
{
    delete ui;
}

void SettingsDialog::updateLineEditState(bool editable)
{
    ui->lineEdit->setEnabled(editable);
    if (!editable) {
        ui->lineEdit->setText(QString::number(Widget::temperatureThreshold));
    }
}

void SettingsDialog::on_pBtn_back_clicked()
{
    this->hide();
    emit backToMainPage();
}

void SettingsDialog::on_pushButton_clicked()
{
    // 解锁 lineEdit
    updateLineEditState(true);
}

void SettingsDialog::on_pushButton_2_clicked()
{
    // 获取 lineEdit 中的值
    bool ok;
    double value = ui->lineEdit->text().toDouble(&ok);

    if (ok && value >= 1 && value <= 100) {
        // 如果输入合法，保存值到 Widget::temperatureThreshold
        Widget::temperatureThreshold = value;
        // 锁定 lineEdit
        updateLineEditState(false);
    } else {
        // 如果输入不合法，弹出警告框
        QMessageBox::warning(this, tr("警告"), tr("请输入合法信息"));
        // 锁定 lineEdit 并恢复原值
        updateLineEditState(false);
    }
}
